import {Component, OnInit} from '@angular/core';
import {NzMessageService} from 'ng-zorro-antd/message';
import {UsedStorePService} from '../used-store-p.service';
import {DatePipe} from '@angular/common';

@Component({
  selector: 'app-hot-search',
  templateUrl: './hot-search.component.html',
  styleUrls: ['./hot-search.component.scss']
})
export class HotSearchComponent implements OnInit {
  STATUS = [
    '未开始',
    '进行中',
    '已删除',
    '已结束',
  ];
  // 表格数据
  listOfData = {
    page: 1,
    total: 0,
    records: []
  };
  tableQuery = {
    page: 1,
    pageSize: 9999
  };
  recycleModelList = [];
  linkList = [];
  hotSearchModal = {
    visible: false,
    id: '',
    word: '',
    hPicUrl: '',
    relationType: [],
    hUrl: '',
    params: '',
    time: [],
  };

  constructor(
    private nzMessageService: NzMessageService,
    private usedStorePService: UsedStorePService,
    private datePipe: DatePipe
  ) {
  }

  ngOnInit(): void {
    this.usedStorePService.getTypeList('').then((res: any) => {
      if (res.code === 0) {
        this.recycleModelList = res.data.records;
      }
    });
    this.usedStorePService.getLinkList().then((res: any) => {
      if (res.code === 0) {
        this.linkList = res.data.records;
      }
    });
    this.getTableData();
  }

  getTableData() {
    this.usedStorePService.getHotSearch(this.tableQuery).then((res: any) => {
      if (res.code === 0) {
        this.listOfData = res.data;
      }
    });
  }

  addHotSearch() {
    this.hotSearchModal.visible = true;
  }

  editHotSearch(data: any) {
    this.hotSearchModal = {
      visible: true,
      id: data.id,
      word: data.word,
      hPicUrl: data.hPicUrl,
      relationType: data.relationType.split(',').map((i: any) => i * 1),
      hUrl: data.hUrl,
      params: data.params,
      // @ts-ignore
      time: [data.beginTime, data.endTime],
    };
  }

  hideHotSearchModal() {
    this.hotSearchModal = {
      visible: false,
      id: '',
      word: '',
      hPicUrl: '',
      relationType: [],
      hUrl: '',
      params: '',
      time: [],
    };
  }

  saveHotSearch() {
    if (!this.hotSearchModal.word) {
      this.nzMessageService.error('请输入名称');
      return;
    }
    if (!this.hotSearchModal.time.length) {
      this.nzMessageService.error('请选择时间');
      return;
    }
    this.usedStorePService.hotSearchEdit({
      id: this.hotSearchModal.id,
      word: this.hotSearchModal.word,
      hPicUrl: this.hotSearchModal.hPicUrl,
      relationType: this.hotSearchModal.relationType.join(','),
      hUrl: this.hotSearchModal.hUrl,
      params: this.hotSearchModal.params,
      beginTime: this.datePipe.transform(this.hotSearchModal.time[0], 'yyyy-MM-dd HH:mm:ss'),
      endTime: this.datePipe.transform(this.hotSearchModal.time[1], 'yyyy-MM-dd HH:mm:ss'),
    }).then(res => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.hideHotSearchModal();
        this.getTableData();
      }
    });
    console.log(this.hotSearchModal);
  }

  linkToRemark(hUrl: string) {
    const item = this.linkList.filter((i: any) => i.key === hUrl);
    if (item.length) {
      return item[0]['remarks'];
    }
    return '';
  }

  move(id: any, type: number) {
    this.usedStorePService.moveHotSearch({id, type}).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.getTableData();
      }
    });
  }

  deleteHotSearch(id: any) {
    this.usedStorePService.deleteHotSearch(id).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.getTableData();
      }
    });
  }
}
